import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    userInfo: {},
    mailType: "all",
    folderInfo: {
      type: "",
      customName: "",
      folderId: "",
    },
    //当前文件夹列表
    currentFolderList: [],
    currentListNextUrl: "",
    checkboxSelectArr: [],
    newShowReply: false,
    createReplyDetail: {},
    isForbidMove: false,
    networkSuccess: true, //是否断网
    inboxUnreadCount: 0,
  },
  getters: {
    userInfo(state) {
      let u = localStorage.getItem("omail_userInfo");
      if (u) {
        u = JSON.parse(u);
      }
      return Object.keys(state.userInfo).length && state.userInfo.mail.length
        ? state.userInfo
        : u || {};
    },
    currentFolderList(state) {
      let l = localStorage.getItem("omail_currentFolderList");
      if (l) {
        l = JSON.parse(l);
      }
      return state.currentFolderList.length ? state.currentFolderList : l || [];
    },
    currentListNextUrl(state) {
      let c = localStorage.getItem("omail_currentListNextUrl");
      if (c) {
        c = JSON.parse(c);
      }
      return state.currentListNextUrl ? state.currentListNextUrl : c || null;
    },
    folderInfo(state) {
      let f = localStorage.getItem("omail_folderInfo");
      if (f) {
        f = JSON.parse(f);
      }
      return Object.keys(state.folderInfo).length > 0 &&
        state.folderInfo.customName.length
        ? state.folderInfo
        : f || {};
    },

    checkboxSelectArr(state) {
      return state.checkboxSelectArr;
    },
    mailType(state) {
      return state.mailType;
    },
    newShowReply(state) {
      return state.newShowReply;
    },
    createReplyDetail(state) {
      return state.createReplyDetail;
    },
    isForbidMove(state) {
      return state.isForbidMove;
    },
    inboxUnreadCount(state) {
      return state.inboxUnreadCount;
    },
  },
  mutations: {
    SAVEUSERINFO(state, userInfo) {
      state.userInfo = userInfo;
      localStorage.setItem("omail_userInfo", JSON.stringify(userInfo));
    },
    SAVEMAILTYPE(state, mailType) {
      state.mailType = mailType;
    },
    SAVECHECKBOXSELECTARR(state, checkboxSelectArr) {
      state.checkboxSelectArr = checkboxSelectArr;
    },

    SAVECURRENTLISTNEXTURL(state, currentListNextUrl) {
      state.currentListNextUrl = currentListNextUrl;
      localStorage.setItem(
        "omail_currentListNextUrl",
        JSON.stringify(currentListNextUrl)
      );
    },

    SAVECURRENTFOLDERLIST(state, currentFolderList) {
      state.currentFolderList = currentFolderList;
      localStorage.setItem(
        "omail_currentFolderList",
        JSON.stringify(currentFolderList)
      );
    },

    SAVEFOLDERINFO(state, folderInfo) {
      state.folderInfo = folderInfo;
      localStorage.setItem("omail_folderInfo", JSON.stringify(folderInfo));
    },
    changeShowReply(state, newShowReply) {
      state.newShowReply = newShowReply;
    },
    forbidMove(state, isForbidMove) {
      state.isForbidMove = isForbidMove;
    },
    SAVECREATEREPLYDETAIL(state, createReplyDetail) {
      state.createReplyDetail = createReplyDetail;
    },
    changeNetworkSuccess(state, networkSuccess) {
      state.networkSuccess = networkSuccess;
    },
    SAVEINBOXUNREADCOUNT(state, inboxUnreadCount) {
      state.inboxUnreadCount = inboxUnreadCount;
    },
  },
  actions: {},
  modules: {},
});
